package registnet.records.source;

import java.util.List;
import java.util.ListIterator;

import registnet.records.CourseTaken;
import registnet.records.Section;
import registnet.records.Student;
import registnet.records.Transcript;
import resources.Constants;

public class TranscriptSource {

	private Student student;
	private Transcript transcript;
	
	public TranscriptSource(Student student){
		this.student = student;
		this.load();
	}
	
public void load() {
		
		this.transcript = new Transcript(this.student);
		
		registnet.dao.Student DAOStudent = new registnet.dao.StudentPeer().getStudent(this.student.getStudentNumber());
		List<registnet.dao.Grade> DAOGrades = new registnet.dao.GradePeer().getGradeByStudent(DAOStudent.getStudentId());
		ListIterator<registnet.dao.Grade> iter = DAOGrades.listIterator();
		while(iter.hasNext()){
			try{
				registnet.dao.Grade DAOGrade = iter.next();
				
				//handle PE 2
				String courseCode = DAOGrade.getCourse().getCourseCode();
				if(courseCode.equals("PE 2"))
					courseCode = courseCode + "-" + DAOGrade.getSectionName().split("-")[0];
				
				//handle courses with ranged number of units
				if(DAOGrade.getSectionName().contains("(")){
					courseCode = courseCode + "-" + DAOGrade.getSectionName().split("[(]")[1].split("[)]")[0];
				}

				CourseTaken course = new CourseTaken(this.student, courseCode, DAOGrade.getSemester(), DAOGrade.getAcademicYear());
				course.setGrade(DAOGrade.getGrade());
				course.setCourseName(new registnet.dao.CoursePeer().getCourse(course.getCourseCode()).getCourseName());
				course.setSection(new Section(DAOGrade.getSectionName()));				
				course.setUnit(DAOGrade.getCourse().getUnits());
				course.setCredit(DAOGrade.getCredit());
				course.setRemarks(DAOGrade.getRemarks());
				
				
				
				if (!DAOGrade.getSectionName().equals("REMOVAL") && !DAOGrade.getSectionName().equals("COMPLETION")){
					this.transcript.addCourse(course);
					
					if (DAOGrade.getGrade() == Constants.MAP_GRADE_KEY.get("4")){
						course.setReExam(new registnet.dao.GradePeer().getRemovalGrade(DAOGrade));
					}
					
					else if (DAOGrade.getGrade() == Constants.MAP_GRADE_KEY.get("INC.")){
						course.setReExam(new registnet.dao.GradePeer().getCompletionGrade(DAOGrade));
					}	
					
				}
					
			}catch(Exception e){}
		}
		
	}
	
	public Transcript getTranscript(){
		return this.transcript;
	}
}
